
import { getDefaultFields } from '../utils';

module.exports = app => {
	const DataTypes = app.Sequelize;

	const Model = app.model.define('rate', {
		id: {
			type: DataTypes.INTEGER(8).UNSIGNED,
			allowNull: false,
			primaryKey: true,
			autoIncrement: true
		},
		grade: {
			type: DataTypes.INTEGER(2),
			allowNull: false,
			defaultValue: 5,
			comment: '评价等级'
		},
		content: {
			type: DataTypes.STRING(50),
			allowNull: false,
			defaultValue: '',
			comment: '评价内容'
		},
		user_id: {
			type: DataTypes.STRING(50),
			allowNull: false,
			defaultValue: '0'
		},
		order_id: {
			type: DataTypes.STRING(100),
			allowNull: false,
			defaultValue: '0'
		},
		...getDefaultFields()
	}, {
		tableName: 'rate',
		timestamps: false
	});

	Model.sync({ alter: true });
	
	Model.associate = function() {
		app.model.Rate.hasOne(app.model.MUser, {
            foreignKey: 'user_id',
            constraints: false,
			sourceKey: 'user_id'
		});
		app.model.Rate.hasOne(app.model.Order, {
            foreignKey: 'order_id',
            constraints: false,
			sourceKey: 'order_id'
		});
	}

	return Model;
};
